package weaver.formmode.customjavacode.browser;

import com.customization.assets.util.DBTools;
import com.customization.assets.util.DateTools;
import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;
import weaver.hrm.User;

import java.util.Map;

/**
 * @author huangwm
 * @desc 关保资产浏览按钮根据当前用户过滤数据
 */
public class GbzcBrowserRight extends AbstractCustomSqlConditionJavaCode {
    private LogTool log = new LogTool("/log/assetlog/huangwm/GbzcBrowserRight",false);
    private static DBTools dbTools = new DBTools();
    private static DateTools dateTools = new DateTools();
    /**
     * 生成SQL查询限制条件
     * @param param
     *  param包含(但不限于)以下数据
     *  user 当前用户
     *
     * @return
     *  返回的查询限制条件的格式举例为: t1.a = '1' and t1.b = '3' and t1.c like '%22%'
     *  其中t1为表单主表表名的别名
     *  param.get("extensionParam") 可以获取所有参数
     */
    @Override
    public String generateSqlCondition(Map<String, Object> param) throws Exception {
        String sqlCondition = " ";
        Map<String, Object> extensionParam = (Map<String, Object>) param.get("extensionParam");//其他参数

        User user = (User)param.get("user");
        String userid = user.getUID()+"";
        String userDep = user.getUserDepartment()+"";
        String userSub = user.getUserSubCompany1()+"";

        if(user==null || userid.equals("")){
            sqlCondition = " 1!=1";
            log.writeLog("关保资产浏览按钮根据当前用户过滤数据>>>当前用户未获取到  不返回数据");
            return sqlCondition;
        }
        boolean isAdmin = dbTools.checkIsAdmin(userid, "42");
        boolean isZsAdmin = dbTools.checkIsAdmin(userid, "45");
        boolean isZsSub = false;
        RecordSet rs = new RecordSet();
        String sql = "select id from HrmDepartment where subcompanyid1 in (select id from HrmSubCompany where id=5 or supsubcomid=5) and id="+userDep;
        log.writeLog("根据当前用户部门id判断是否属于中山海关下的科室");
        rs.execute(sql);
        if (rs.next()){
            log.writeLog("当前用户属于中山海关");
            isZsSub = true;
        }

        if (isAdmin || userid.equals("1")){
            sqlCondition = " 1=1 and kpzt not in (6,8)";
            log.writeLog("当前用户id "+userid+" 属于系统管理员 sqlCondition="+sqlCondition);
            return sqlCondition;
        }
        if(isZsAdmin || isZsSub){
            sqlCondition = " sybm in (select id from HrmSubCompany where id=5 or supsubcomid=5) and kpzt not in (6,8)";
            log.writeLog("当前用户id "+userid+" 属于中山海关 sqlCondition="+sqlCondition);
            return sqlCondition;
        }
        sqlCondition = " sybm="+userSub+" and kpzt not in (6,8)";
        log.writeLog("当前用户id "+userid+" 不属于中山海关或系统管理员 sqlCondition="+sqlCondition);


        return sqlCondition;
    }
}
